package com.yanfan.zutai.controller;


import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceWarn;
import com.yanfan.zutai.domain.BDeviceZtBind;
import com.yanfan.zutai.domain.BWorkOrder;
import com.yanfan.zutai.domain.vo.BDviceOrderWarnVo;
import com.yanfan.zutai.service.BCommonService;
import com.yanfan.zutai.service.IBDeviceWarnService;
import com.yanfan.zutai.service.IBWorkOrderService;
import com.yanfan.zutai.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 设备报警Controller
 *
 * @author ruoyi
 * @date 2021-02-27
 */
@RestController
@RequestMapping("/zutai/bDeviceWarn")
public class BDeviceWarnController extends BaseController {
    @Autowired
    private IBDeviceWarnService bDeviceWarnService;
    @Autowired
    private IBWorkOrderService bWorkOrderService;
    @Autowired
    private BCommonService bCommonService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询设备报警列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BDeviceWarn bDeviceWarn) {
        startPage();
        List<String> imeiList = new ArrayList<>();
        if (Detect.notEmpty(bDeviceWarn.getZtGuid())) {
            String ztSql = "select * from b_device_zt_bind where zt_guid=?";
            BeanPropertyRowMapper<BDeviceZtBind> bindBeanPropertyRowMapper = new BeanPropertyRowMapper<>(BDeviceZtBind.class);
            List<BDeviceZtBind> ztBinds = jdbcTemplate.query(ztSql, bindBeanPropertyRowMapper, bDeviceWarn.getZtGuid());
            for (int i = 0; i < ztBinds.size(); i++) {
                imeiList.add(ztBinds.get(i).getDeviceMac());
            }
            bDeviceWarn.setImeiList(imeiList);
        }
        List<BDeviceWarn> list = bDeviceWarnService.selectBDeviceWarnList(bDeviceWarn);
        for (int i = 0; i < list.size(); i++) {
            BDeviceWarn deviceWarn = list.get(i);
            deviceWarn.setWarnNowTime(deviceWarn.getWarnTime());
        }
        if (bDeviceWarn.getWarnAngle() != null && list.size() > 0) {
            BDeviceWarn bDeviceWarnQuery = list.get(0);
            bDeviceWarnQuery.setWarnAngle(1);
            bDeviceWarnService.updateBDeviceWarn(bDeviceWarnQuery);
            bDeviceWarnQuery.setWarnMessage(bDeviceWarnQuery.getDeviceName() + "的" + bDeviceWarnQuery.getWarnMessage());
        }
        return getDataTable(list);
    }

    /**
     * 导出设备报警列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:export')")
    @Log(title = "设备报警", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(BDeviceWarn bDeviceWarn) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<BDeviceWarn> list = bDeviceWarnService.selectBDeviceWarnList(bDeviceWarn);
        ExcelUtil<BDeviceWarn> util = new ExcelUtil<BDeviceWarn>(BDeviceWarn.class);
        return util.exportExcel(list, "warn");
    }

    /**
     * 获取设备报警详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bDeviceWarnService.selectBDeviceWarnById(id));
    }

    /**
     * 新增设备报警
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:add')")
    @Log(title = "设备报警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BDeviceWarn bDeviceWarn) {
        return toAjax(bDeviceWarnService.insertBDeviceWarn(bDeviceWarn));
    }

    /**
     * 修改设备报警
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:edit')")
    @Log(title = "设备报警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BDeviceWarn bDeviceWarn) {
        return toAjax(bDeviceWarnService.updateBDeviceWarn(bDeviceWarn));
    }

    /**
     * 删除设备报警
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:remove')")
    @Log(title = "设备报警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bDeviceWarnService.deleteBDeviceWarnByIds(ids));
    }

    /**
     * 批量处理预警工单
     */
    @PostMapping(value = "/batchHandleWarn/{ids}")
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceWarn:batchDeal')")
    public AjaxResult batchHandleWarn(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i < ids.length; i++) {
            BDeviceWarn bDeviceWarn = bDeviceWarnService.selectBDeviceWarnById(ids[i]);
            bDeviceWarn.setShutPeople(loginUser.getUser().getNickName());
            bDeviceWarn.setShutTime(new Date());
            bDeviceWarn.setProcessState(1);
            bDeviceWarnService.updateBDeviceWarn(bDeviceWarn);
        }
        return AjaxResult.success();
    }

    /**
     * 工单分配报警记录
     */
    @PostMapping(value = "/allocationHandleWarn")
    public AjaxResult allocationHandleWarn(@RequestBody BDviceOrderWarnVo bDviceOrderWarnVo) {
        for (int i = 0; i < bDviceOrderWarnVo.getWarnIds().length; i++) {
            BDeviceWarn bDeviceWarn = bDeviceWarnService.selectBDeviceWarnById(bDviceOrderWarnVo.getWarnIds()[i]);
            bDeviceWarn.setProcessState(1);
            bDeviceWarn.setWorkOrderGuid(bDviceOrderWarnVo.getOrderGuid());
            bDeviceWarnService.updateBDeviceWarn(bDeviceWarn);
        }
        if (bDviceOrderWarnVo.getWarnIds().length > 0) {
            BWorkOrder bWorkOrder = new BWorkOrder();
            bWorkOrder.setGuid(bDviceOrderWarnVo.getOrderGuid());
            List<BWorkOrder> bWorkOrders = bWorkOrderService.selectBWorkOrderList(bWorkOrder);
            bWorkOrder = bWorkOrders.get(0);
            bWorkOrder.setProcessState(1);
            bWorkOrderService.updateBWorkOrder(bWorkOrder);
        }
        return AjaxResult.success();
    }

    /**
     * 工单移除报警记录
     */
    @PostMapping(value = "/removeHandleWarn")
    public AjaxResult removeHandleWarn(@RequestBody BDviceOrderWarnVo bDviceOrderWarnVo) {
        for (int i = 0; i < bDviceOrderWarnVo.getWarnIds().length; i++) {
            BDeviceWarn bDeviceWarn = bDeviceWarnService.selectBDeviceWarnById(bDviceOrderWarnVo.getWarnIds()[i]);
            bDeviceWarn.setProcessState(0);
            bDeviceWarn.setWorkOrderGuid(" ");
            bDeviceWarnService.updateBDeviceWarn(bDeviceWarn);
        }
        BDeviceWarn bDeviceWarnQuery = new BDeviceWarn();
        bDeviceWarnQuery.setWorkOrderGuid(bDviceOrderWarnVo.getOrderGuid());
        List<BDeviceWarn> bDeviceWarns = bDeviceWarnService.selectBDeviceWarnList(bDeviceWarnQuery);
        if (bDeviceWarns.size() == 0) {
            BWorkOrder bWorkOrder = new BWorkOrder();
            bWorkOrder.setGuid(bDviceOrderWarnVo.getOrderGuid());
            List<BWorkOrder> bWorkOrders = bWorkOrderService.selectBWorkOrderList(bWorkOrder);
            bWorkOrder = bWorkOrders.get(0);
            bWorkOrder.setProcessState(0);
            bWorkOrderService.updateBWorkOrder(bWorkOrder);
        }
        return AjaxResult.success();
    }

    /**
     * 安卓报警生成工单
     */
    @PostMapping(value = "/insertWarnOrder")
    public AjaxResult insertWarnOrder(@RequestBody BDviceOrderWarnVo bDviceOrderWarnVo) {
        String workOrderGuid = UUID.randomUUID().toString();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        BDeviceWarn bDeviceWarn = bDeviceWarnService.selectBDeviceWarnById(bDviceOrderWarnVo.getWarnId());
        bDeviceWarn.setProcessState(1);
        bDeviceWarn.setWorkOrderGuid(workOrderGuid);
        bDeviceWarnService.updateBDeviceWarn(bDeviceWarn);

        BWorkOrder bWorkOrder = new BWorkOrder();
        // bWorkOrder.setDeptIdStrs(bDeviceWarn.getDeptIdStrs());
        bWorkOrder.setDeviceMac(bDeviceWarn.getDeviceMac());
        bWorkOrder.setDeviceName(bDeviceWarn.getDeviceName());
        bWorkOrder.setDeviceAddress(bDeviceWarn.getAddress());
        bWorkOrder.setWarnType(bDeviceWarn.getWarnType());
        bWorkOrder.setGuid(UUID.randomUUID().toString());
        bWorkOrder.setCreateBy(loginUser.getUsername());
        bWorkOrder.setCreateTime(new Date());
        bWorkOrder.setUpdateBy(loginUser.getUsername());
        bWorkOrder.setUpdateTime(new Date());
        bWorkOrder.setType("工单管理");
        String workNumber = bCommonService.getWorkNumber("工单管理", loginUser.getUser().getDeptId() + "");
        bWorkOrder.setGuid(workOrderGuid);
        bWorkOrder.setWordOrderNumber(workNumber);
        bWorkOrder.setProcessState(0);
        bWorkOrder.setWorkOrderMessage(bDviceOrderWarnVo.getWorkOrderMessage());
        bWorkOrder.setWorkOrderRemark(bDviceOrderWarnVo.getWorkOrderRemark());
        bWorkOrderService.insertBWorkOrder(bWorkOrder);
        return AjaxResult.success();
    }
}
